package com.poetize.plus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poetize.plus.entity.dto.LabelDTO;
import com.poetize.plus.entity.dto.page.LabelPageDTO;
import com.poetize.plus.entity.po.Label;
import com.poetize.plus.entity.vo.LabelVO;
import com.poetize.plus.entity.vo.SelectVO;
import com.poetize.plus.entity.vo.page.LabelPageVO;
import com.poetize.plus.mapper.LabelMapper;
import com.poetize.plus.service.LabelService;
import com.poetize.plus.utils.BaseConstant;
import com.poetize.plus.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @Author zuosy
 * @Date 2024/3/30 16:53
 **/
@Service
@RequiredArgsConstructor
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {

    private final RedisUtil redisUtil;

    @Override
    public Page<LabelPageVO> labelPage(LabelPageDTO labelPageDTO) {
        Page<Label> labelPage = lambdaQuery()
                .eq(Label::getSortId, labelPageDTO.getSortId())
                .like(StringUtils.hasText(labelPageDTO.getLabelName()), Label::getLabelName, labelPageDTO.getLabelName())
                .page(Page.of(labelPageDTO.getCurrent(), labelPageDTO.getSize()));
        Page<LabelPageVO> result = new Page<>();
        if (CollectionUtils.isEmpty(labelPage.getRecords())){
            return result;
        }
        result.setTotal(labelPage.getTotal());
        result.setRecords(LabelPageVO.convertPage(labelPage.getRecords()));
        return result;
    }

    @Override
    public Boolean addLabel(LabelDTO labelDTO) {
        Label checkLabelName = lambdaQuery()
                .eq(Label::getLabelName, labelDTO.labelName())
                .one();
        if (Objects.nonNull(checkLabelName)){
            throw new IllegalArgumentException("该标签已存在");
        }
        Label label = LabelDTO.convertDTO(labelDTO);
        boolean insert = baseMapper.insert(label) > 0;
        if (!insert){
            throw new IllegalArgumentException("数据异常");
        }
        redisUtil.setHomeRedis(BaseConstant.Redis.HOME_LABEL, this::selectHomeLabel);
        return true;
    }

    @Override
    public LabelVO queryLabel(Long id) {
        Label label = baseMapper.selectById(id);
        return Optional.ofNullable(label)
                .map(LabelVO::convert)
                .orElseThrow(() -> new IllegalArgumentException("数据异常"));
    }

    @Override
    public Boolean updateLabel(LabelDTO labelDTO) {
        Label checkLabelName = lambdaQuery()
                .eq(Label::getLabelName, labelDTO.labelName())
                .ne(Label::getLabelId, labelDTO.labelId())
                .one();
        if (Objects.nonNull(checkLabelName)){
            throw new IllegalArgumentException("该标签已存在");
        }
        Label label = LabelDTO.convertDTO(labelDTO);
        boolean update = baseMapper.updateById(label) > 0;
        if (!update){
            throw new IllegalArgumentException("数据异常");
        }
        redisUtil.setHomeRedis(BaseConstant.Redis.HOME_LABEL, this::selectHomeLabel);
        return true;
    }

    @Override
    public Boolean deleteSort(Long id) {
        boolean delete = baseMapper.deleteById(id) > 0;
        if (!delete){
            throw new IllegalArgumentException("数据异常");
        }
        redisUtil.setHomeRedis(BaseConstant.Redis.HOME_LABEL, this::selectHomeLabel);
        return true;
    }

    @Override
    public List<SelectVO> labelSelect(Long sortId) {
        List<Label> labelList = lambdaQuery()
                .eq(Objects.nonNull(sortId), Label::getSortId, sortId)
                .list();
        return SelectVO.convertLabelSelect(labelList);
    }

    @Override
    public List<LabelVO> getHomeLabel() {
        return redisUtil.getHomeRedis(BaseConstant.Redis.HOME_LABEL, this::selectHomeLabel);
    }

    /**
     * 标签数据
     * @return
     */
    private List<LabelVO> selectHomeLabel() {
        List<Label> labelList = lambdaQuery().list();
        return LabelVO.convertList(labelList);
    }
}
